package com.tianfeng.news.ui.adapter.recyclerview;

import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tianfeng.news.R;
import com.tianfeng.news.model.bean.NewsItemBean;
import com.tianfeng.news.model.bean.news.SearchBean;
import com.tianfeng.news.ui.activity.base.BaseActivity;
import com.tianfeng.news.ui.activity.news.NewsDetailActivity2;
import com.tianfeng.news.utils.ImageUtils;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Created by Dinosa on 2018/1/18.
 */

public class SearchNewsAdapter extends BaseMultiItemQuickAdapter<SearchBean.DataBean.NewsBean, BaseViewHolder> {


    private final OnlyTextViewHolder mOnlyTextViewHolder;
    private final OnlyOneImgViewHolder mOnlyOneImgViewHolder;
    private final ThreeImgViewHolder mThreeImgViewHolder;
    private final BaseActivity mActivity;


    public SearchNewsAdapter(BaseActivity activity, List<SearchBean.DataBean.NewsBean> data) {
        super(data);
        mActivity = activity;
        addItemType(NewsItemBean.ONLY_TEXT, R.layout.item_home_only_text);
        addItemType(NewsItemBean.ONLY_ONE_IMG, R.layout.item_home_img_one);
        addItemType(NewsItemBean.ONLY_THREE_IMG, R.layout.item_home_img_three);


        mOnlyTextViewHolder = new OnlyTextViewHolder();
        mOnlyOneImgViewHolder = new OnlyOneImgViewHolder();
        mThreeImgViewHolder = new ThreeImgViewHolder();

    }

    @Override
    protected void convert(final BaseViewHolder helper, final SearchBean.DataBean.NewsBean item) {

        doItemClick(helper, item);

        switch (item.getItemType()) {
            case NewsItemBean.ONLY_TEXT:

                mOnlyTextViewHolder.init(helper, item);
                break;

            case NewsItemBean.ONLY_ONE_IMG:
                mOnlyOneImgViewHolder.init(helper, item);
                break;

            case NewsItemBean.ONLY_THREE_IMG:
                mThreeImgViewHolder.init(helper, item);
                break;
        }
    }
    protected void doItemClick(BaseViewHolder helper, final SearchBean.DataBean.NewsBean item) {


        //这里我们要做的一个操作就是;
        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mActivity.startActivity(NewsDetailActivity2.getIntent(mActivity,item.getUrl()
                        ,item.getUrlmd5() , "xiaohua" , 1));
            }
        });
    }

    public class OnlyTextViewHolder {
        public void init(BaseViewHolder holder, SearchBean.DataBean.NewsBean item) {
            holder.setText(R.id.tv_title, item.getTopic());

            holder.setText(R.id.tv_source, item.getSource());
            holder.setText(R.id.tv_time, item.getDate());

        }
    }

    public class OnlyOneImgViewHolder {
        public void init(BaseViewHolder holder, SearchBean.DataBean.NewsBean item) {
            holder.setText(R.id.tv_news_title, item.getTopic());

            ImageUtils.loadImage(mActivity, item.getMiniimg().get(0).getSrc(), (ImageView) holder.getView(R.id.iv_news_img));

            holder.setText(R.id.tv_source, item.getSource());
            holder.setText(R.id.tv_time, item.getDate());
        }
    }

    public class ThreeImgViewHolder {
        public void init(BaseViewHolder holder, SearchBean.DataBean.NewsBean item) {

            holder.setText(R.id.tv_news_title, item.getTopic());

            ImageUtils.loadImage(mActivity, item.getMiniimg().get(0).getSrc(), (ImageView) holder.getView(R.id.iv_img1));
            ImageUtils.loadImage(mActivity, item.getMiniimg().get(1).getSrc(), (ImageView) holder.getView(R.id.iv_img2));
            ImageUtils.loadImage(mActivity, item.getMiniimg().get(2).getSrc(), (ImageView) holder.getView(R.id.iv_img3));

            holder.setText(R.id.tv_source, item.getSource());
            holder.setText(R.id.tv_time, item.getDate());
            holder.setText(R.id.tv_time, item.getDate());
            int random = 0;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                random = ThreadLocalRandom.current().nextInt(80, 1000);
                holder.setText(R.id.tv_money1,"已发"+random+""+"元");
            }
        }
    }
}